package com.ftdigital.primemovers_operators.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.model.DayInventory;


/**
 * Created by Curry on 2015-12-16.
 */
public class DayInvertoryListViewAdapter extends RecyclerView.Adapter<DayInvertoryListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private DayInventory dayInventory;
    public DayInvertoryListViewAdapter(Context context, DayInventory dayInventory) {
        mContext = context;
        this.dayInventory = dayInventory;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public DayInvertoryListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.day_inventory_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DayInvertoryListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.getOilName().setText(dayInventory.getData().get(position).getGas_type());
        holder.getStationName().setText(dayInventory.getData().get(position).getName());
        holder.getInventory().setText(dayInventory.getData().get(position).getQty());
        holder.getTime().setText(dayInventory.getData().get(position).getTime());
    }

    @Override
    public int getItemCount() {
        return dayInventory.getData().size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView oilName;
        private TextView inventory;
        private TextView stationName;
        private TextView time;
        public ViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.time);
            oilName = (TextView) itemView.findViewById(R.id.oil_name);
            inventory = (TextView) itemView.findViewById(R.id.inventory);
            stationName = (TextView) itemView.findViewById(R.id.station_name);
        }

        public TextView getOilName() {
            return oilName;
        }

        public void setOilName(TextView oilName) {
            this.oilName = oilName;
        }

        public TextView getInventory() {
            return inventory;
        }

        public void setInventory(TextView inventory) {
            this.inventory = inventory;
        }

        public TextView getStationName() {
            return stationName;
        }

        public void setStationName(TextView stationName) {
            this.stationName = stationName;
        }

        public TextView getTime() {
            return time;
        }

        public void setTime(TextView time) {
            this.time = time;
        }
    }
}
